﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQData;
using VQWEB.common;
using System.Reflection;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for QuizHandler
    /// </summary>
    public class hn_QuizHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Quiz
            string add_quiz = context.Request["add_quiz"];
            if (!string.IsNullOrEmpty(add_quiz))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_quiz>(add_quiz);
                if (obj.chuyennganh_array_id != null)
                    bussiness.InsertAllInOne<hn_quiz_chuyennganh>(obj.chuyennganh_array_id, "chuyennganh_id", "quiz_id", obj.quiz_id);
              context.Response.Write("Thực thi thành công !");
                
            }
            string view_quiz = context.Request["view_quiz"];
            if (!string.IsNullOrEmpty(view_quiz))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingle(context, Convert.ToInt32(Id));
                else
                    RenderChuyennganh(context);
            }
            string list_quiz = context.Request["list_quiz"];
            if (!string.IsNullOrEmpty(list_quiz))
            {
                 RenderList(context);
            }
            string delete_quiz = context.Request["delete_quiz"];
            if (!string.IsNullOrEmpty(delete_quiz))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.Delete<hn_quiz>(delete_quiz);
                RenderList(context);
            }

            string lockunlock_quiz = context.Request["lockunlock_quiz"];
            if (!string.IsNullOrEmpty(lockunlock_quiz))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_quiz>(lockunlock_quiz, "quiz_active");
                RenderList(context);
            }

            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void RenderChuyennganh(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> business = new BusinessLayer<HuongNghiepModelDataContext>();
            var chuyennganh = business.RenderArrayCheckboxJSON(HuongNghiepData.ViewChuyennganh(),null, "id", "ten", "chuyennganh_array_id[]");

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("chuyennganh", chuyennganh);

            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderSingle(HttpContext context, int Id)
        {
            BusinessLayer<HuongNghiepModelDataContext> business = new BusinessLayer<HuongNghiepModelDataContext>();
            object obj = business.Get<hn_quiz>(
               q => q.quiz_id.Equals(Id),
               q => new
               {
                   id =q.quiz_id,
                   value = q.quiz_value,
                   noidung =q.quiz_noidung,
                   active = q.quiz_active
               });

            PropertyInfo info = obj.GetType().GetProperty("id");
            var value = info.GetValue(obj, null);

            List<object> array_chuyennganh_id = business.List<hn_quiz_chuyennganh>(
                null,
                null,
                n => n.quiz_id.Equals(value),
                null,
                n => n.chuyennganh_id
                ).ToList();
            List<Dictionary<string, object>> listchuyennganh = business.RenderArrayCheckboxJSON(HuongNghiepData.ViewChuyennganh(), array_chuyennganh_id, "id", "ten", "chuyennganh_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("quiz", obj);
            dic.Add("chuyennganh", listchuyennganh);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context)
        {
            DataLayer<HuongNghiepModelDataContext> layer = new DataLayer<HuongNghiepModelDataContext>();
            IList<object> list = layer.List<hn_quiz>(
                null,
                null,
                null,
                null,
                 q => new
                 {
                     id = q.quiz_id,
                     value = q.quiz_value,
                     noidung = q.quiz_noidung,
                     active = q.quiz_active
                 });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}